﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cofe.Core;
using Cofe.Core.Property;

namespace Cofe.Core.Logger
{

    public class CofeSystemEventArgs : EventArgs
    {
        public static string LogSyntax = "({0}) - {1}";

        public string VolumeId { get; protected set; }
        public string RelativePath { get; protected set; }
        public ModifiedAction ChangeType { get; protected set; }

        /// <summary>
        /// If ChangeType = ActionCalled, the ActionId (see CofeProoperties)
        /// </summary>
        public int? ActionId { get; protected set; }

        /// <summary>
        /// Full parse path of the changed entries.
        /// </summary>
        public virtual string FullParsePath { get; protected set; }
        public virtual bool IsFolder { get; protected set; }
        public virtual string Name { get { return PathFE.GetFileName(FullParsePath); } }

        public override string ToString()
        {
            return String.Format(LogSyntax, ChangeType, FullParsePath);
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return ToString().Equals(obj.ToString());
        }

        public static CofeSystemEventArgs FromVolIdAndRelPath(string volId, string path, bool isFolder, 
            ModifiedAction action, int? actionId = null)
        {
            return new CofeSystemEventArgs(action, isFolder, PathFE.FormatFullParseName(volId, path), actionId);
        }

        public static CofeSystemEventArgs FromParsePath(string fullParsePath, bool isFolder, ModifiedAction action, 
            int? actionId = null)
        {
            return new CofeSystemEventArgs(action, isFolder, fullParsePath, actionId);
        }

        public static CofeSystemEventArgs FromPropertyHost(IPropertyHost ph, ModifiedAction action, int? actionId = null)
        {
            string fullParsePath = ph.Behaviors.GetProperty<string>(CofeProperties.FullParseName);
            bool isFolder = ph.SupportedDefinitions.Contains(Cofe.Core.PropertyDefinitions.CofeDirDefinitions,
                PropertyDefinitionComparer.CompareCategory);
            return FromParsePath(fullParsePath, isFolder, action, actionId);
        }

        protected CofeSystemEventArgs(ModifiedAction changeType, bool isFolder, string fullParsePath, int? actionId)
        {
            if (actionId != null)
                ActionId = actionId;
            ChangeType = changeType; 
            IsFolder = isFolder;
            FullParsePath = fullParsePath;

            string relPath, VolId;
            PathFE.ParseFullParseName(fullParsePath, out VolId, out relPath);
            VolumeId = VolId;
            RelativePath = relPath;
        }
    }

    public class CofeRenameEventArgs : CofeSystemEventArgs
    {
        public virtual string OldParsePath { get; protected set; }

        protected CofeRenameEventArgs(ModifiedAction changeType, bool isFolder, string parsePath,
            string oldParsePath)
            : base(changeType, isFolder, parsePath, null)
        { OldParsePath = oldParsePath; }

        public static CofeRenameEventArgs FromParsePath(string orgParsePath, string newParsePath, bool isFolder, ModifiedAction action)
        {
            return new CofeRenameEventArgs(action, isFolder, newParsePath, orgParsePath);
        }


        public override string ToString()
        {
            return String.Format(LogSyntax, ChangeType, OldParsePath + " >> " + FullParsePath);
        }
    }

    public class ErrorEventArgs : EventArgs
    {
        public Exception Error { get; private set; }

        public ErrorEventArgs(Exception error)
        {
            Error = error;
        }

        public override bool Equals(object obj)
        {
            return false;
        }        
    }
}
